package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.AirSecurityVehicleDao;
import com.kun.portal.webframe.po.AirSecurityVehiclePo;
import com.kun.portal.webframe.po.ContractManagePo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-08-01.
 */
@Service
public class AirSecurityVehicleService {
    @Autowired
    private AirSecurityVehicleDao airSecurityVehicleDao;
    public List<AirSecurityVehiclePo> findAllAirSecurityVehiclePo(){
        Sort sortx = new Sort(new Sort.Order(Sort.Direction.ASC,"vehicleType"));
        return airSecurityVehicleDao.findAll(sortx);
    }
    public  List<AirSecurityVehiclePo> checkASV(String vehicleType , String vehicleState  ){

        Specification query = new Specification<AirSecurityVehiclePo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<AirSecurityVehiclePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(vehicleType)){
                    pre.add(cb.like(root.get("vehicleType"), "%"+vehicleType+"%"));
                }
                if(StringUtils.isNotBlank(vehicleState)){
                    pre.add(cb.like(root.get("vehicleState"), "%"+vehicleState+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return airSecurityVehicleDao.findAll(query);

    }
    public void saveASV(AirSecurityVehiclePo airSecurityVehiclePo){
        airSecurityVehicleDao.save(airSecurityVehiclePo);

    }
    public void deleteASV(AirSecurityVehiclePo airSecurityVehiclePo){
        airSecurityVehicleDao.delete(airSecurityVehiclePo);
    }
}
